package io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

import org.junit.Ignore;
import org.junit.Test;

public class BasicFileOutputMY {
	static String file = "BasicFileOutput.out";

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new StringReader(
				BufferedInputFile.read("BasicFileOutput.java")));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				file)));
		int lineCount = 1;
		String s;
		while ((s = in.readLine()) != null)
			out.println(lineCount++ + ": " + s);
		out.close();
		// Show the stored file:
		System.out.println(BufferedInputFile.read(file));
	}

	
	@Test
	public void test002() throws IOException {
		
		String file2 = "BasicFileOutput.out";
		
		BufferedReader in = new BufferedReader(new StringReader(
				BufferedInputFile.read( getJavaFilePath() + "BasicFileOutput.java")));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				file2)));
		int lineCount = 1;
		String s;
		while ((s = in.readLine()) != null)
			out.println(lineCount++ + ": " + s);
		out.close();
		// Show the stored file:
		System.out.println(BufferedInputFile.read(file2));
	}

	/**
	 * 输出： C:/wxg/git3/thinkinginjava/thinkinginjava/src/main/java/io/
	 */
	@Test
	@Ignore
	public void test001() {
		System.out.println(getJavaFilePath());
	}

	private static String getJavaFilePath() {
		StringBuilder sbb = new StringBuilder();
		String rootpath = BasicFileOutputMY.class.getClassLoader()
				.getResource("").getPath();
		String pkg = BasicFileOutputMY.class.getPackage().getName();
		// rootpath = rootpath.replaceAll("target[/\\\\]test-classes[/\\\\]",
		// "");
		rootpath = rootpath.replaceAll("\\\\", "/");
		rootpath = rootpath.replaceFirst("/", "");
		rootpath = rootpath.replaceAll("target/test-classes/", "");
		sbb.append(rootpath).append("src/main/java/")// .append("src/test/java/")
				.append(pkg).append("/");
		rootpath = sbb.toString();
		return rootpath;
	}
}
